package com.food.restaurantpro.fragments;

import java.util.ArrayList;

import com.food.restaurantpro.R;
import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.activity.MainActivity;
import com.food.restaurantpro.adapters.PromotionListViewAdapter;
import com.food.restaurantpro.database.tables.PromotionTable;
import com.food.restaurantpro.managingnetwork.ManagingNetwork;
import com.food.restaurantpro.objects.Promotion;
import com.food.restaurantpro.utils.AlertDialogInterface;
import com.food.restaurantpro.utils.AlertDialogManager;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class PromotionListViewFragment extends Fragment {

	private TextView tvHeader;
	private ArrayList<Promotion> mPromotionArrayList;
	private ListView mListView;
	private PromotionListViewAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_promotion, container,
				false);

		tvHeader = (TextView) view.findViewById(R.id.tvHeader);
		if (tvHeader != null) {
			tvHeader.setText(R.string.tvPromotion_Title);
		}
		mListView = (ListView) view.findViewById(R.id.lvPromotion);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Promotion promotion = (Promotion) mListView
						.getItemAtPosition(position);
				((MainActivity) getActivity())
						.showPromotionDetailFragment(promotion);
			}
		});

		updateGallery();
		return view;
	}

	private void updateGallery() {
		if (mPromotionArrayList == null) {
			mPromotionArrayList = PromotionTable.getAllPromotions();
		}
		if (mPromotionArrayList.size() > 0) {
			mAdapter = new PromotionListViewAdapter(getActivity(),
					mPromotionArrayList);
			mListView.setAdapter(mAdapter);
		} else {

			String messageError = "";
			if (ManagingNetwork.isNetworkAvailable()) {
				messageError = RestaurantApp.CURRENT_CONTEXT
						.getString(R.string.SERVER_ERROR);
			} else {
				messageError = RestaurantApp.CURRENT_CONTEXT
						.getString(R.string.NETWORK_ERROR);
			}
			AlertDialogManager.showAlertDialog(getActivity(),
					new AlertDialogInterface() {

						@Override
						public void onChooseYesCallback() {
							// TODO Auto-generated method stub
						}

						@Override
						public void onChooseNoCallback() {
							// TODO Auto-generated method stub

						}
					}, false, true, RestaurantApp.CURRENT_CONTEXT
							.getString(R.string.title_internet_error),
					messageError, RestaurantApp.CURRENT_CONTEXT
							.getString(R.string.ok), "", false);
		}
	}

	@Override
	public void onDestroy() {
		// TODO delete image and cache de giai phong bo nho
		super.onDestroy();
		mListView.setAdapter(null);
		if (mAdapter != null) {
			mAdapter.clearMemoryCache();
		}
		if (mPromotionArrayList != null) {
			mPromotionArrayList.clear();
			mPromotionArrayList = null;
		}
		Runtime.getRuntime().gc();
	}
}
